<?php
namespace app\seller\controller;
use think\Input;
use think\facade\Request;
class Financial extends Common{
    public function initialize(){
        parent::initialize();  
    }
    // 结算间
    public function index(){
        if(request()->isPost()){
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $shopid=session('seller.id');
            if($shopid == 23 || $shopid == 25 || $shopid == 29 || $shopid == 30){
                $list=db('settle')->where('shopid',$shopid)->order('id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
                foreach ($list['data'] as $k=>$v){
                    $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']); 
                    $list['data'][$k]['money_t'] = $v['money_a']-$v['money_s'];  
                    $list['data'][$k]['money_s']='-'.$v['money_s']; 
                    if($v['state']===0){ 
                        $list['data'][$k]['money_t']='<span class="red">'.$list['data'][$k]['money_t'].'</span>'; 
                        $list['data'][$k]['state_name']='<span class="red">待付款</span>'; 
                    }else{
                        $list['data'][$k]['state_name']='<span class="gre">已付款</span>'; 
                    }
                    if($v['type']=='user'){
                        $list['data'][$k]['typename']='码商'; 
                        $list['data'][$k]['shopname']=$v['username']; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['typename']='代理'; 
                        $list['data'][$k]['shopname']=$v['username']; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['typename']='商户'; 
                        $list['data'][$k]['username']=$v['susername']; 
                        $list['data'][$k]['mobile']=$v['smobile']; 
                    }
                }
                return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
            }else{
                $list=db('jiesuan')->where('shopid',$shopid)->order('id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
                foreach ($list['data'] as $k=>$v){
                    $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']); 
                    $list['data'][$k]['money_t'] = $v['money_a']-$v['money_s'];  
                    $list['data'][$k]['money_s']='-'.$v['money_s']; 
                    if($v['state']===0){ 
                        $list['data'][$k]['money_t']='<span class="red">'.$list['data'][$k]['money_t'].'</span>'; 
                        $list['data'][$k]['state_name']='<span class="red">待付款</span>'; 
                    }else{
                        $list['data'][$k]['state_name']='<span class="gre">已付款</span>'; 
                    }
                    if($v['type']=='user'){
                        $list['data'][$k]['typename']='码商'; 
                        $list['data'][$k]['shopname']=$v['username']; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['typename']='代理'; 
                        $list['data'][$k]['shopname']=$v['username']; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['typename']='商户'; 
                        $list['data'][$k]['username']=$v['susername']; 
                        $list['data'][$k]['mobile']=$v['smobile']; 
                    }
                }
                return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
            }     
        }
        // 结算汇总
        if(session('seller.id') == 23 || session('seller.id') == 25 || session('seller.id') == 29 || session('seller.id') == 30){
             $payorder=[];
            $payorder['total']=db('apporder')->where('shopid',session('seller.id'))->where('state',2)->sum('money');
            $payorder['income']=db('apporder')->where('shopid',session('seller.id'))->where('state',2)->sum('fei_shop');
            $payorder['settle']=$payorder['total']-$payorder['income'];
            $payorder['num']=count(db('apporder')->where('shopid',session('seller.id'))->where('state',2)->select()); 
        }else{
            $payorder=[];
            $payorder['total']=db('channel_order')->where('shopid',session('seller.id'))->where('state',2)->sum('money');
            $payorder['income']=db('channel_order')->where('shopid',session('seller.id'))->where('state',2)->sum('fei_shop');
            $payorder['settle']=$payorder['total']-$payorder['income'];
            $payorder['num']=count(db('channel_order')->where('shopid',session('seller.id'))->where('state',2)->select()); 
        }      
        $this->assign('payorder',$payorder);
        $this->assign('title','结算明细');
        return $this->fetch();
    } 
}